package com.qingjiang.projectschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingjiang.projectschedule.mapper.ManageDeptMapper;
import com.qingjiang.projectschedule.pojo.Department;
import com.qingjiang.projectschedule.pojo.ManageDept;
import com.qingjiang.projectschedule.pojo.ShowAllManageDepts;
import com.qingjiang.projectschedule.service.DepartmentService;
import com.qingjiang.projectschedule.service.ManageDeptService;
import com.qingjiang.projectschedule.service.UserInfoService;
import com.qingjiang.projectschedule.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jx
 * @description 针对表【manage_dept】的数据库操作Service实现
 * @createDate 2024-03-26 10:36:52
 */
@Service
public class ManageDeptServiceImpl extends ServiceImpl<ManageDeptMapper, ManageDept>
        implements ManageDeptService {
    @Autowired
    private ManageDeptMapper manageDeptMapper;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private DepartmentService departmentService;

    @Override
    public Result getAllManageList() {
        LambdaQueryWrapper<ManageDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ManageDept::getUid).groupBy(ManageDept::getUid);
        List<Object> users = manageDeptMapper.selectObjs(queryWrapper);
        List<ShowAllManageDepts> res = new ArrayList<>();
        for (Object user : users) {
            Integer uid = (Integer) user;
            String userName = userInfoService.getUserNameById(uid);
            LambdaQueryWrapper<ManageDept> wrapper = new LambdaQueryWrapper<>();
            wrapper.select(ManageDept::getDeptId).eq(ManageDept::getUid, uid);
            List<Object> deptIds = manageDeptMapper.selectObjs(wrapper);
            Department dept = departmentService.getDeptById((Integer) deptIds.get(0));
            ShowAllManageDepts allManageDepts = new ShowAllManageDepts(uid, userName, dept.getDeptId(), dept.getOriginId(), dept.getDeptType(), dept.getDeptName());
            if (deptIds.size() > 1) {
                List<ShowAllManageDepts> manageDepts = new ArrayList<>();
                for (int i = 1; i < deptIds.size(); i++) {
                    Department childDept = departmentService.getDeptById((Integer) deptIds.get(i));
                    ShowAllManageDepts child = new ShowAllManageDepts(uid, userName, childDept.getDeptId(), childDept.getOriginId(), childDept.getDeptType(), childDept.getDeptName());
                    manageDepts.add(child);
                }
                allManageDepts.setChildren(manageDepts);
            }
            res.add(allManageDepts);
        }
        return Result.ok(res);
    }

    @Override
    public Result addLeaderConfig(Integer uid, Integer deptId) {
        if (deptId == -1) return addLeaderManageAllDept(uid);
        LambdaQueryWrapper<ManageDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(ManageDept::getDeptId).eq(ManageDept::getUid, uid);
        List<Object> deptIds = manageDeptMapper.selectObjs(wrapper);
        if (deptIds.contains(deptId)) return Result.build(null, 201, "该领导已有该部门权限", false);
        String deptName = departmentService.getDeptNameById(deptId);
        ManageDept manageDept = new ManageDept(uid, deptId, deptName);
        manageDeptMapper.insert(manageDept);
        return Result.ok(null);
    }

    @Override
    public Result addLeaderManageAllDept(Integer uid) {
        LambdaQueryWrapper<ManageDept> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(ManageDept::getDeptId).eq(ManageDept::getUid, uid);
        List<Object> deptIds = manageDeptMapper.selectObjs(wrapper);
        List<Department> deptList = departmentService.getDeptList();
        for (Department department : deptList) {
            if (!deptIds.contains(department.getDeptId())) {
                ManageDept manageDept = new ManageDept(uid, department.getDeptId(), department.getDeptName());
                manageDeptMapper.insert(manageDept);
            }
        }
        return Result.build(null, 200, "添加成功", true);
    }

    @Override
    public Result deleteLeaderConfig(Integer uid, Integer deptId) {
        LambdaUpdateWrapper<ManageDept> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ManageDept::getUid, uid);
        if (deptId != -1) {
            updateWrapper.eq(ManageDept::getDeptId, deptId);
        }
        manageDeptMapper.delete(updateWrapper);
        return Result.build(null, 200, "撤销成功", true);
    }
}




